/**
 * @version Create on 2012-11-1
 * @author Yinzi Chen
 */

public class BinaryTreeToLinkedList {

	class TreeNode {
		int val;
		TreeNode left;
		TreeNode right;

		TreeNode(int x) {
			val = x;
		}
	}

	public TreeNode buildLinkList(TreeNode root) {
		TreeNode last = root;
		if (root.left != null) {
			last = buildLinkList(root.left);
			last.right = root.right;
			root.right = root.left;
			root.left = null;
		}
		if (root.right != null)
			last = buildLinkList(root.right);
		return last;
	}

	public void flatten(TreeNode root) {
		if (root == null)
			return;
		buildLinkList(root);
	}

	public static void main(String[] args) {

	}

}
